package com.sxd.bill.rela.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sxd.bill.common.item.LabelItem;
import com.sxd.bill.common.item.Pairs;
import com.sxd.bill.common.item.TextLabelItem;

public class Spliter {
    /**
     * 按索引位置切分文本内容与标签
     * 
     * @param textLabelItem
     * @param split_idx
     * @return
     */
    public static Pairs<TextLabelItem, TextLabelItem> split(TextLabelItem textLabelItem, int split_idx) {
        String text = textLabelItem.getText();
        List<LabelItem> labelItems = textLabelItem.getLabelItems();
        // 分割文本
        String leftText = text.substring(0, split_idx);
        String rightText = text.substring(split_idx, text.length());
        // 分割标签
        ArrayList<LabelItem> leftLabels = new ArrayList<>();
        ArrayList<LabelItem> rightLabels = new ArrayList<>();
        for (LabelItem labelItem : labelItems) {
            LabelItem newLabelItem = labelItem.clone();
            if (labelItem.getEndIndex() <= split_idx) {
                leftLabels.add(newLabelItem);
            } else {
                int startIdx = labelItem.getStartIndex() - split_idx;
                int endIdx = labelItem.getEndIndex() - split_idx;
                newLabelItem.setStartIndex(startIdx);
                newLabelItem.setEndIndex(endIdx);
                try {
                    newLabelItem.setLabelEntity(rightText.substring(startIdx, endIdx));
                } catch (Exception e) {
                    System.out.print("\n------------\ntext: " + text + "\n splitIdx : " + split_idx + " start: "
                            + startIdx + " end: "
                            + endIdx + "\n enity: " + labelItem.getLabelEntity());
                    newLabelItem.setLabelEntity(labelItem.getLabelEntity());
                }

                rightLabels.add(newLabelItem);
            }
        }
        // 构造返回结果
        TextLabelItem leftTextLabelItem = new TextLabelItem(leftText, leftLabels);
        TextLabelItem rightTextLabelItem = new TextLabelItem(rightText, rightLabels);
        return new Pairs<TextLabelItem, TextLabelItem>(leftTextLabelItem, rightTextLabelItem);
    }

    /**
     * 将消息内容拆分为发布者和消息文本
     * 如“邮储云南 张丽君 : 1、代友收6月双国，清单报价呀” 将被拆为“邮储云南 张丽君 : ”和“1、代友收6月双国，清单报价呀”
     * 
     * @param textLabelItem
     * @return
     */
    public static Pairs<TextLabelItem, TextLabelItem> splitPublisherVsMessage(TextLabelItem textLabelItem) {
        String text = textLabelItem.getText();
        int idx = text.indexOf(":", 0);
        return split(textLabelItem, idx + 1);
    }

    /**
     * 文本内容分段
     * 
     * @param textLabelItem
     * @return
     */
    public static List<TextLabelItem> splitSegments(TextLabelItem textLabelItem) {
        ArrayList<TextLabelItem> splitItems = new ArrayList<TextLabelItem>();

        String text = textLabelItem.getText();
        Pattern pattern = Pattern
                .compile("([\\r\\n]+|[；，。\\s][另再]?(?=出|收)|.{3,}[,，。、；\\s](?=[^,，。、；\\s]{0,5}量?[出收买卖]断?(点|少量|：|\\d+)))");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            int indx = matcher.end();
            Pairs<TextLabelItem, TextLabelItem> pairsItem = split(textLabelItem, indx);

            TextLabelItem leftItem = pairsItem.getFirst();
            TextLabelItem rightItem = pairsItem.getSecond();
            // 去掉文本尾部的空白符
            String leftText = leftItem.getText().replaceAll("\\s+$", "");
            String rightText = rightItem.getText().replaceAll("\\s+$", "");
            leftItem.setText(leftText);
            rightItem.setText(rightText);
            // 递归继续往下切分
            splitItems.addAll(splitSegments(leftItem));
            splitItems.addAll(splitSegments(rightItem));
        } else {
            splitItems.add(textLabelItem);
        }
        return splitItems;
    }

}
